<?php
if (isset($_SESSION['errMessage'])) {
?>
<font style="color: red; font-weight: bold">
    <?php echo $_SESSION['errMessage'] ?>
<font>
<?php    
}
?>
<table width="100%" border="0" cellpadding="2" cellspacing="2">
    <?php
    $maxRowPerPage = 2;
    $nCol = 3; // initial
    $nRow = 1; // initial
    $maxRow = 0; // loop
    $nPage = 1; //initial
    if ($AllBarang) {
        if ($TotalRow > $nCol) {
            $nCek = $TotalRow % $nCol;
            if ($nCek > 0) {
                $nCek1 = $TotalRow - $nCek;
                $nRow = ($nCek1 / $nCol) + 1;
            } else {
                $nRow = $TotalRow / $nCol;
            }
        } else if ($TotalRow < $nCol) {
            $nCol = $TotalRow;
        }
        if ($nRow > $maxRowPerPage) {
            $maxRow = $maxRowPerPage;
        } else {
            $maxRow = $nRow;
        }
        $nCounter = 1;
        for ($rRow=1;$rRow<=$maxRow;$rRow++) {
    ?>
            <tr>
    <?php
                for ($cCol=1;$cCol<=$nCol;$cCol++) {
                    $InfoBrgByRownum = mysql_fetch_array($AllBarang);
                    $rownum = $InfoBrgByRownum[0];
                    $IDBarang = $InfoBrgByRownum[1];
                    $Jenis = $InfoBrgByRownum[2];
                    $Nama = $InfoBrgByRownum[3];
                    $Keterangan = $InfoBrgByRownum[4];
                    $Ukuran = $InfoBrgByRownum[5];
                    $Warna = $InfoBrgByRownum[6];
                    $TglMasuk = $InfoBrgByRownum[7];
                    $Harga = number_format($InfoBrgByRownum[8],2,",",".");
                    $Jumlah = $InfoBrgByRownum[9];
                    $Supplier = $InfoBrgByRownum[10];
                    $AlamatSupplier = $InfoBrgByRownum[11];
                    $TelpSupplier = $InfoBrgByRownum[12];
                    $EmailSupplier = $InfoBrgByRownum[13];

                    $Tahun = substr($TglMasuk,0,4);
                    $Bulan = substr($TglMasuk,5,2);
                    $Tanggal = substr($TglMasuk,8,2);
                    $ConvTanggal = $Tanggal." ".fnConvertMonth($Bulan)." ".$Tahun;
    ?>
                    <td>
                        <table border="1">
                            <tr valign="top">
                                <td>
                                    <img src="images/back2.jpg" width="100px" height="100px"/>
                                </td>
                                <td>
                                    <?php echo $rownum ?>&nbsp;/&nbsp;
                                    <?php echo $IDBarang ?><br>
                                    <?php echo $Nama ?><br>
                                    <?php echo $Ukuran ?><br>
                                    <?php echo $Warna ?><br>
                                    Rp&nbsp;<?php echo $Harga ?>
                                </td>
                            </tr>
                            <?php 
                            if (isset($_SESSION['loginSts'])) {
                            ?>
                            <tr>
                                <td colspan="2">
                                    <input type="checkbox" name="rdBuy" id="rdBuy" value="<?php echo $IDBarang ?>">Buy This
                                </td>
                            </tr>
                            <?php
                            }
                            ?>
                        </table>
                    </td>
    <?php
                    $nCounter = $nCounter + 1;
                    if ($nCounter > $TotalRow) {
                        break;
                    }
                }
    ?>
            </tr>
    <?php
        }
        $nCurPage = 1;
        if (isset($_SESSION['curPage'])) {
            $nCurPage = $_SESSION['curPage'];
        }
        $nRecordPerPage = $nCol * $maxRow;
        if ($TotalRow < $nRecordPerPage) {
            // jumlah data lebih kecil dari daya tampung halaman
            $nPage = 1;
        } else {
            // jumlah data lebih besar dari daya tampung halaman
            $nCekPage = $TotalRow % $nRecordPerPage;
            if ($nCekPage > 0) {
                $nCekPage1 = $TotalRow - $nCekPage;
                $nPage = ($nCekPage1 / $nRecordPerPage) + 1;
            } else {
                $nPage = $TotalRow / $nRecordPerPage;
            }
        }
        $idToBuy = '';
        if (isset($_SESSION['checkToBuy'])) {
            $idToBuy = $_SESSION['checkToBuy'];
        }
    ?>
            <tr>
                <td align="center" colspan="<?php echo $nCol ?>">
                    <button type="button" name="btFirst" id="btFirst" onclick="fnFirstCatalog()">First</button>
                    <button type="button" name="btPrev" id="btPrev" onclick="fnPrevCatalog()">Prev</button>
                    <input class="myHidden" type="text" name="txtCurPage" id="txtCurPage" size="2" value="<?php echo $nCurPage ?>"/>
                    &nbsp;dari&nbsp;
                    <input class="myHidden" type="text" name="txtTotalPage" id="txtTotalPage" size="2" value="<?php echo $nPage ?>"/>
                    <button type="button" name="btNext" id="btNext" onclick="fnNextCatalog()">Next</button>
                    <button type="button" name="btLast" id="btLast" onclick="fnLastCatalog()">Last</button>
                    <button type="button" name="btCheckOut" id="btCheckOut" onclick="fnCheckOut()">Check Out</button>
                    <input type="text" name="idToBuy" id="idToBuy" value="<?php echo $idToBuy ?>"/>
                    
                </td>
            </tr>
            <tr class="myHidden" >
                <td align="center" colspan="<?php echo $nCol ?>">
                    <input type="text" name="txtRecordPerPage" id="txtRecordPerPage" size="3" value="<?php echo $nRecordPerPage ?>"/>
                    &nbsp;dari&nbsp;
                    <input type="text" name="txtTotalRow" id="txtTotalRow" size="3" value="<?php echo $TotalRow ?>"/>
                </td>
            </tr>
    <?php
    }
    ?>
</table>
